/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.pacman.net.bluetooth.server;

import com.sun.perseus.platform.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

/**
 * This class performs all the operations for bluetooth server.
 * @author fredy
 */
public class BluetoothServer
{
    private BluetoothServerListener listener;

    /**
     * Creates a new instance of BluetoothServer.
     * @param listener the BluetoothServerListener
     */
    public BluetoothServer(BluetoothServerListener listener)
    {
        this.listener = listener;
    }

    /**
     * Receives the message. This method doesn't block.
     */
    public void receiveMessage()
    {
        Thread t = new Thread(new BluetoothServerRunnable());
        t.start();
    }

    class BluetoothServerRunnable implements Runnable
    {
        public void run()
        {
            receive();
        }

        private void receive()
        {
            StreamConnectionNotifier notifier = null;
            StreamConnection conn = null;
            InputStream is = null;
            ByteArrayOutputStream os = null;
            try
            {
                LocalDevice local = LocalDevice.getLocalDevice();
                if (!local.setDiscoverable(DiscoveryAgent.GIAC))
                {
                    throw new RuntimeException("Failed to change to the discoverable mode");
                }
                notifier = (StreamConnectionNotifier) Connector.open(
                    "btspp://localhost:86b4d249fb8844d6a756ec265dd1f6a3");
                conn = notifier.acceptAndOpen();
                is = new BufferedInputStream(conn.openInputStream());
                os = new ByteArrayOutputStream();
                int data;
                while ((data = is.read()) != -1)
                {
                    os.write(data);
                }
                os.flush();

                listener.received(new String(os.toByteArray()));
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
                throw new RuntimeException(ex.getMessage());
            }
            finally
            {
                try
                {
                    if (is != null)
                    {
                        is.close();
                    }
                    if (os != null)
                    {
                        os.close();
                    }
                    if (conn != null)
                    {
                        conn.close();
                    }
                    if (notifier != null)
                    {
                        notifier.close();
                    }
                }
                catch (IOException ex)
                {
                    ex.printStackTrace();
                    throw new RuntimeException(ex.getMessage());
                }
            }
        }
    }
}
